package models.mes

import play.api.Logging
import play.api.db.slick.{DatabaseConfigProvider, HasDatabaseConfigProvider}
import slick.basic.DatabaseConfig
import slick.jdbc.JdbcProfile

import java.sql.Timestamp
import javax.inject.Inject
import scala.concurrent.{ExecutionContext, Future}

//class MesOrdersRepository @Inject()()(implicit executionContext: ExecutionContext)
//  extends MesOrdersTableTrait with Logging {
//  override val dc = DatabaseConfig.forConfig[JdbcProfile]("slick.dbs.default")
//
//  import dc.profile.api._
//  private val db = dc.db

class MesOrdersRepository @Inject()(
                                     protected val dbConfigProvider: DatabaseConfigProvider
                                   )(implicit executionContext: ExecutionContext)
  extends MesOrdersTableTrait with HasDatabaseConfigProvider[JdbcProfile] with Logging {

  import profile.api._

  private val prdOrdersRec = TableQuery[MesProductOrdersRecTable]
  private val stkOrdersRec = TableQuery[MesStockOrdersRecTable]

  def prdFindByMo(mo: String) = db.run {
    if (mo.length > 30) prdOrdersRec.filter(_.rowid === mo).map(f => f).result
    else prdOrdersRec.filter(_.mo === mo).map(f => f).result
  }

  def prdFindPPBomByMo(mo: String) = db.run {
    prdOrdersRec.filter(_.mo === mo).map(f => f.ppbom).result.headOption
  }

  def prdFindMoByPPBom(ppbom: String) = db.run {
    prdOrdersRec.filter(_.ppbom === ppbom).map(f => f.mo).result.headOption
  }

  def prdMoSetSendSign(rowid: String, sign: Int, result: String) = db.run {
    if (sign == 0) {
      prdOrdersRec.filter(_.rowid === rowid).map(
        f => (f.mo_send, f.mo_result, f.ppbom_send)
      ).update((sign, result, sign))
    }
    else {
      prdOrdersRec.filter(_.rowid === rowid).map(
        f => (f.mo_send, f.mo_result)
      ).update((sign, result))
    }
  }

  def prdPPBomSetSendSign(ppbom: String, sign: Int, result: String) = db.run {
    prdOrdersRec.filter(_.ppbom === ppbom).map(
      f => (f.ppbom_send, f.ppbom_result)
    ).update((sign, result))
  }

  def prdUpInsert(order: MesProductOrdersRec) = db.run {
    prdOrdersRec.filter(_.rowid === order.rowid).map(f => f).result
  }.flatMap {
    exists => {
      if (exists.nonEmpty) {
        db.run {
          prdOrdersRec.filter(_.rowid === order.rowid)
            .map(f => (f.mo_status, f.mo_modify, f.mo_approve, f.status, f.ppbom_approve))
            .update((order.mo_status, order.mo_modify, order.mo_approve, order.status, order.ppbom_approve))
        }
      } else {
        db.run {
          prdOrdersRec += order
        }
      }
    }

  }

  def prdUpPPBomInfo(ppbom: String, mo: String, moId: Int, status: String, approveDate: Timestamp) = db.run {
    prdOrdersRec.filter(_.fentity === moId).filter(_.mo === mo)
      .map(f => (f.ppbom, f.ppbom_status, f.ppbom_approve))
      .update((ppbom, status, approveDate))
  }

  def prdFindPPBom() = db.run {
    prdOrdersRec.filter(_.ppbom_send =!= 1)
      .filter(_.mo_send === 1)
      .filterNot(_.status inSet Set("0", "1", "2"))
      //      .filter(_.status inSet Set("3","4"))
      .map(f => f).result
  }

  def stkSetSendFalse(code: String) = db.run {
    stkOrdersRec.filter(_.code === code).map(f => f.send).update(0)
  }

  def stkUpInsert(order: MesStockOrdersRec) = db.run {
    stkOrdersRec.filter(_.code === order.code).map(f => f).result
  }.flatMap {
    exists => {
      logger.debug("exists: " + exists.mkString)
      if (exists.nonEmpty) {
        val data = exists.head
        val updata = data.copy(
          date = order.date,
          modify = order.modify,
          approve = order.approve,
          status = order.status
        )
        if (data.approve.isEmpty) db.run {
          stkOrdersRec
            .filter(_.code === order.code)
            .map(f => f)
            .update(updata)
        }
        Future(if (data.send == 1) true else false)
      } else {
        db.run(
          //          (stkOrdersRec returning stkOrdersRec.map(_.send)) += order
          stkOrdersRec += order
        ).map(f => if (f == 1) false else true)
      }

    }
  }

  def stkSetSendSign(code: String, sign: Int, result: String = "") = db.run {
    stkOrdersRec.filter(_.code === code).map(f => (f.send, f.result)).update((sign, result))
  }
}
